package levelrewind.android.app;

import java.util.ArrayList;

import levelrewind.android.app.R;
import levelrewind.android.app.model.PictureHandler;
import levelrewind.android.app.model.RestDataHandler;
import levelrewind.android.app.model.Util;
import levelrewind.android.app.model.Util.Emotion;
import levelrewind.android.app.vo.Category;
import levelrewind.android.app.vo.Person;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;

import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class AppStartActivity extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_app_start);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        initView();
        downloadData();
    }
    
    private void initView() {
    	TextView text = (TextView)findViewById(R.id.loading_text);
//    	ImageView logo = (ImageView)findViewById(R.id.game_logo);
    	
    	text.setTypeface(Util.customFont(this));
//    	RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//    	params.addRule(RelativeLayout.ABOVE, R.id.start_table);
//    	logo.setLayoutParams(params);
    }
    
    @Override
    public void onBackPressed() {
    }
    
    private void downloadData()
    {
    	if (Util.isOnline(this.getApplicationContext())) 
        { 
    		Log.i(this.getClass().getSimpleName(), "STATUS:ONLINE");
    		
    		//PictureHandler.clearAllPictureFromDevice(this);

    		new DownloadDataTask().execute();
        	Log.i(this.getClass().getSimpleName(), "Downloaded data to device");
        }
    	else
    	{
    		Log.i(this.getClass().getSimpleName(), "STATUS:OFFLINE");
			retryDialog().show();
		}
    	
    }
    
    private AlertDialog.Builder retryDialog()
    {
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.no_network);
        builder.setMessage(R.string.con_fail);
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				startMainActivity();
			}
		});
        builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				downloadData();
			}
		});
        builder.create();
        return builder;
    }
	
	private void startMainActivity() {
		Intent myIntent = new Intent(AppStartActivity.this, MainActivity.class);
		startActivity(myIntent);
		finish();
	}
	
    private class DownloadDataTask extends AsyncTask<Void, Void, Void>
    {
//    	View icon = (View)findViewById(R.id.loading_view);

//    	protected void onPreExecute() { 
//    		icon.setVisibility(View.VISIBLE);
//    		text.setVisibility(View.VISIBLE);
//    		text.setTypeface(Util.customFont(AppStartActivity.this));
    	
//    	}
    	
		protected void onPostExecute(Void voids) {
//			icon.setVisibility(View.INVISIBLE);
//			text.setVisibility(View.INVISIBLE);
			startMainActivity();
    	}
    	
		@Override
		protected Void doInBackground(Void...voids ) {
			RestDataHandler handler = new RestDataHandler(AppStartActivity.this);
			PictureHandler pHandler = new PictureHandler(AppStartActivity.this);
			boolean res = false;
			while (!res) {	
				//if there is error when downloadData with return false 
				//We try to download it again until res=true
				res = handler.downloadData();
			}
			
			Category[] cList = handler.getCategoriesFromDevice();
			for(Category c : cList) {
				Person[] pList = handler.getPersonsFromDevice(c.getId());
				if (pList != null) {
					for (Person p : pList){
						if (p.getPicPath() != null && p.getPicPath().size() != 0) {
							ArrayList<Drawable> dList = pHandler.downloadPicture(p.getPicPath().get(Emotion.NORMAL).getPath());
							if (dList != null) {
								for (Drawable d : dList) {
									pHandler.savePictureToDevice(d, pHandler.getFileName(p, Emotion.NORMAL));
								}
							}
						}
					}	
				}
			}
			
			return null;
		}
    }
}
